
//
//  MYChangeMachineDetailViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/5/14.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYChangeMachineDetailViewController.h"
#import "MY_getVendingListHttpApi.h"

@interface MYChangeMachineDetailViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic,strong) NSMutableDictionary        *dicData;
@property (nonatomic,weak) IBOutlet   UITextField     *fieldMachineNumber;
@property (nonatomic,weak) IBOutlet   UILabel         *labControlNumber;
@property (nonatomic,weak) IBOutlet   UILabel         *labpoint;
@property (nonatomic,weak) IBOutlet   UITextField     *fieldnote;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;

@end

@implementation MYChangeMachineDetailViewController

-(instancetype)initWithMachineDetailDic:(NSDictionary *)dictionary
{
    self = [super initWithNibName:@"MYChangeMachineDetailViewController" bundle:nil];
    if (self) {
        [self.dicData removeAllObjects];
        [self.dicData addEntriesFromDictionary:dictionary];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=11.0) {
        
    }else{
        // 针对 11.0 以下的iOS系统进行处理
        for (NSLayoutConstraint *constraint in self.viewHead.superview.constraints) {
            if (constraint.firstItem == self.viewHead && constraint.firstAttribute == NSLayoutAttributeTop) {
                constraint.constant = 20;
            }
        }
        
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignRespond)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
    [self setDetailDataToView];
}

- (void)setDetailDataToView
{
    NSString *number = [NSString stringWithFormat:@"%@",[self.dicData objectForKey:@"number"]];
    if (![number stringIsValid]) {
        number = @"";
    }
    [self.fieldMachineNumber setText:number];
    NSString *controlNumber = [NSString stringWithFormat:@"%@",[self.dicData objectForKey:@"controlNumber"]];
    if (![controlNumber stringIsValid]) {
        controlNumber = @"";
    }
    [self.labControlNumber setText:controlNumber];
    NSString *pointName = [NSString stringWithFormat:@"%@",[self.dicData objectForKey:@"pointName"]];
    if (![pointName stringIsValid]) {
        pointName = @"";
    }
    [self.labpoint setText:pointName];
    
}

- (void)resignRespond
{
     [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableDictionary *)dicData
{
    if (_dicData == nil) {
        _dicData = [[NSMutableDictionary alloc] init];
    }
    return _dicData;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

- (IBAction)onBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onChangeButtonPressed:(id)sender
{
    [self.fieldMachineNumber becomeFirstResponder];
}

- (IBAction)onSureButtonPressed:(id)sender
{
    NSString *strNumber = self.fieldMachineNumber.text;
    NSString *info = self.fieldnote.text;
    NSString *dicNumber = [NSString stringWithFormat:@"%@",[self.dicData objectForKey:@"number"]];
    if ([dicNumber stringIsValid] && [strNumber isEqualToString:dicNumber]) {
        UIAlertView *alertDialog = [[UIAlertView alloc] initWithTitle:nil message:@"没有修改数据。" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertDialog show];
        return;
    }
    
    NSMutableDictionary *dicppost = [[NSMutableDictionary alloc] init];
    [dicppost setObject:strNumber forKey:@"number"];
    [dicppost setObject:info forKey:@"remarks"];
    
    NSString *strid = [NSString stringWithFormat:@"%@",[self.dicData objectForKey:@"id"]];
    [MY_getVendingListHttpApi myUpdataVendingMachineDetailWithID:strid PostData:dicppost complationHandler:^(OCJBaseResponceModel *responseModel) {
        if ([responseModel.ocjStr_code isEqualToString:@"0"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
}

@end
